//
//  HWPromoteGameViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/2/26.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWPromoteGameViewController.h"
#import "HWPromoteGameCell.h" // 本试图单元格
#import "HWPromoteGameModel.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>

#import "WXApi.h"

#import "HWerweiMaFenXiangView.h"


@interface HWPromoteGameViewController ()<UITableViewDelegate,UITableViewDataSource,HWPromoteGameCellDelegate> {
    
     SendMessageToQQReq *req;
    
    
}
@property(strong,nonatomic) UITableView *mainTabelView;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSMutableArray *dataMuArr;

@end

@implementation HWPromoteGameViewController

- (UITableView *)mainTabelView {
    
    if (_mainTabelView == nil) {
        _mainTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _mainTabelView.delegate = self;
        _mainTabelView.dataSource = self;
        _mainTabelView.backgroundColor = beijingyanSe;
        _mainTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _mainTabelView;
    
    
    
    
    
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self setupUI];
    [self setupNavigateView];
    
    [self addrefreshControl];
    
    [self addrefreshControl];
    
    
    
    // 发送网络请求
  //  [self sendNetworkRequest];
    
}




- (void)addrefreshControl {
   
    self.mainTabelView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
   [self.mainTabelView.mj_header beginRefreshing];
    
    self.mainTabelView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}




// 下拉刷新
- (void)underRefresh {
      self.p = @"1";
      NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
      NSString *uid =  [HWLocallyStoredData getLoginUserID];
      NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
      NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
      NSDictionary *parametes = @{              @"ios" : @"1",
                                           @"p" :self.p,
                                           @"ct" : @"app",
                                            @"ac" : @"mygames",
                                            @"t" : tStr,
                                            @"uid":uid,
                                            @"sign" :signStrMd5
                                            };
            
            
                [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                      
                       [self.mainTabelView.mj_header endRefreshing];
                       [self.mainTabelView.mj_footer endRefreshing];

                      NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                      if ([codeStr intValue] == 1) {
                          
                          
                          self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                          
                          self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                          self.dataMuArr = [HWPromoteGameModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                          [self.mainTabelView reloadData];
                           NSLog(@"%@",response);
                    
                          
                           
                      } else {
                          
                        [MBProgressHUD showError:response[@"message"]];
                                        
                    
                          
                      }
                      
                 
                      
                  } failure:^(NSString *error) {
                     // [SVProgressHUD dismiss];
                      [self.mainTabelView.mj_header endRefreshing];
                      [self.mainTabelView.mj_footer endRefreshing];
                      
                      
//                       [MBProgressHUD showSuccess:@"登录失败"];
//                       [MBProgressHUD hideHUD];
                      
                      
                  }];
            
        
    
      
    
    


}





// 上拉加载

- (void)onRefresh {
    
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
         
          [self.mainTabelView.mj_header endRefreshing];
          [self.mainTabelView.mj_footer endRefreshing];

         [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
        return ;
                       
      }
    
        NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
          NSString *uid =  [HWLocallyStoredData getLoginUserID];
          NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
          NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
          NSDictionary *parametes = @{              @"ios" : @"1",
                                               @"p" :self.p,
                                               @"ct" : @"app",
                                                @"ac" : @"mygames",
                                                @"t" : tStr,
                                                @"uid":uid,
                                                @"sign" :signStrMd5
                                                };
                
                
                    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
                          
                           [self.mainTabelView.mj_header endRefreshing];
                           [self.mainTabelView.mj_footer endRefreshing];

                          NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
                          if ([codeStr intValue] == 1) {
                              
                              
                              self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                              self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                              
                              
                              NSMutableArray *mutableArr = [HWPromoteGameModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
                              for (int i = 0; i < mutableArr.count; i++) {


                                  [self.dataMuArr addObject:mutableArr[i]];

                               }
                              
                              
                              [self.mainTabelView reloadData];
                         
                                NSLog(@"%@",response);
                              
                    
                              
                               
                          } else {
                              
                            [MBProgressHUD showError:response[@"message"]];
                                            
                        
                              
                          }
                          
                     
                          
                      } failure:^(NSString *error) {
                         // [SVProgressHUD dismiss];
                          [self.mainTabelView.mj_header endRefreshing];
                          [self.mainTabelView.mj_footer endRefreshing];
                          
                          
    //                       [MBProgressHUD showSuccess:@"登录失败"];
    //                       [MBProgressHUD hideHUD];
                          
                          
                      }];
                
            
    
    
    
        // 申请游戏
                          
    
}

















- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  self.dataMuArr.count;
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
    
    HWPromoteGameCell  *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWPromoteGameCell" owner:nil options:nil].firstObject;
        
    }
    
    cell.delegate = self;
    
    HWPromoteGameModel *model =    self.dataMuArr[indexPath.row];
    cell.model = model;
    
   
        
    return  cell;

    
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  168;
    
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    
    
//   NSString *utf8String = @"https://v.qq.com/x/cover/rfgkivnu2k0ylyb/i0013m4f0zt.html";
//   NSString *title = @"这是一个标题";
//   NSString *description = @"内容内容内容内容内容测试测试测试测试测试";
//   NSString *previewImageUrl = @"https://imgsa.baidu.com/forum/w%3D580/sign=a45e927533d12f2ece05ae687fc3d5ff/c6ed5adf8db1cb1396e6cfb1d854564e93584b4f.jpg";
//   QQApiNewsObject *newsObj = [QQApiNewsObject
//                               objectWithURL:[NSURL URLWithString:utf8String]
//                               title:title
//                               description:description
//                               previewImageURL:[NSURL URLWithString:previewImageUrl]];
//    req = [SendMessageToQQReq reqWithContent:newsObj];
//
//    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    
    
    
    
    


    
}





// 设置导航栏
- (void)setupNavigateView {
    
    self.navigationItem.title = @"推广游戏";
    
}


- (void)setupUI {
    
    self.view.backgroundColor = beijingyanSe;
    [self.view addSubview:self.mainTabelView];
    
    
    
    
    
    
    
}




- (void)HWPromoteGameCellxunzhong:(NSInteger)xuanzh PromoteGameModel:(HWPromoteGameModel *_Nullable)modle {
    
    
    
    

        if (xuanzh == 3) {


            [MBProgressHUD showSuccess:@"复制成功"];

            // 用来复制
                 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                 pasteboard.string =  modle.h5_url;


            return;

        }


          HWerweiMaFenXiangView  *fenxiang = [[HWerweiMaFenXiangView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
            fenxiang.urlStr = modle.h5_url;
            [kKeyWindow addSubview:fenxiang];
    
         return;
        
    
    
    
    if (xuanzh == 0) {
        
        
        
        SDWebImageManager *manager =[SDWebImageManager sharedManager];
      //根据URLString去下载图片
        NSString *URLString = modle.icon;
       __weak typeof(self) weakSelf = self;
        [manager loadImageWithURL:[NSURL URLWithString:URLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            
            if (finished) {
                
                NSLog(@" shuju %@",image);
                
                
                
                    NSString *utf8String = modle.h5_url;
                      NSString *title = modle.name;
                      NSString *description = [NSString stringWithFormat:@"%@%@",modle.name,modle.type];
                      
                      //创建发送对象实例
                      SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                      sendReq.bText = NO;//不使用文本信息
                      sendReq.scene = 0;
                      // 发送到聊天界面  0
                      // 发送到朋友圈    1
                      // 发送到微信收藏  2
                      //创建分享内容对象
                      WXMediaMessage *urlMessage = [WXMediaMessage message];
                      urlMessage.title = title;//分享标题 urlMessage.description = @"";
                      urlMessage.description= description;
                      
                      //分享描述
                      [urlMessage setThumbImage:image];
                      //分享图片,使用SDK的setThumbImage方法可压缩图片大小
                      //创建多媒体对象
                      WXWebpageObject *webObj = [WXWebpageObject object];
                      webObj.webpageUrl = utf8String;//分享链接
                    
                      //完成发送对象实例
                      urlMessage.mediaObject = webObj;
                      sendReq.message = urlMessage;
                      //发送分享信息
                      [WXApi sendReq:sendReq];
                
                
                
                
                
                
                
            }
            
            
        }];
        
        
        
        
      
        // 申请游戏
        
        
        
        
        
         
    } else if (xuanzh == 1) {
        
        NSString *utf8String = modle.h5_url;
         NSString *title = modle.name;
        NSString *description = [NSString stringWithFormat:@"%@%@",modle.name,modle.type];
        
         NSString *previewImageUrl = modle.icon ;
         QQApiNewsObject *newsObj = [QQApiNewsObject
                                     objectWithURL:[NSURL URLWithString:utf8String]
                                     title:title
                                     description:description
                                     previewImageURL:[NSURL URLWithString:previewImageUrl]];
          req = [SendMessageToQQReq reqWithContent:newsObj];
          
          QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        
        
        
        
        
    }else if (xuanzh == 2) {
        
        
        
         SDWebImageManager *manager =[SDWebImageManager sharedManager];
        //根据URLString去下载图片
         NSString *URLString = modle.icon;
        __weak typeof(self) weakSelf = self;
          [manager loadImageWithURL:[NSURL URLWithString:URLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
              
          } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
              
              if (finished) {
                  
                  NSLog(@" shuju %@",image);
                  
                  
                  
                        NSString *utf8String = modle.h5_url;
                        NSString *title = modle.name;
                        NSString *description = [NSString stringWithFormat:@"%@%@",modle.name,modle.type];
                        
                        //创建发送对象实例
                        SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                        sendReq.bText = NO;//不使用文本信息
                        sendReq.scene = 1;
                        // 发送到聊天界面  0
                        // 发送到朋友圈    1
                        // 发送到微信收藏  2
                        //创建分享内容对象
                        WXMediaMessage *urlMessage = [WXMediaMessage message];
                        urlMessage.title = title;//分享标题 urlMessage.description = @"";
                        urlMessage.description= description;
                        
                        //分享描述
                        [urlMessage setThumbImage:image];
                        //分享图片,使用SDK的setThumbImage方法可压缩图片大小
                        //创建多媒体对象
                        WXWebpageObject *webObj = [WXWebpageObject object];
                        webObj.webpageUrl = utf8String;//分享链接
                      
                        //完成发送对象实例
                        urlMessage.mediaObject = webObj;
                        sendReq.message = urlMessage;
                        //发送分享信息
                        [WXApi sendReq:sendReq];
                  
                  
                  
                  
                  
                  
                  
              }
              
              
          }];
          
        
        
    }else if (xuanzh == 3) {
        
        // 用来复制
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string =  modle.h5_url;
        
        
        
        
    }
    
}
- (void)shared:(NSInteger)type Icon:(NSString *)icon H5Url:(NSString *)h5_url Name:(NSString *)name{
    if (type == 0) {
        SDWebImageManager *manager =[SDWebImageManager sharedManager];
        //根据URLString去下载图片
        NSString *URLString = icon;
        __weak typeof(self) weakSelf = self;
        [manager loadImageWithURL:[NSURL URLWithString:URLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            
            if (finished) {
                
                NSLog(@" shuju %@",image);
                NSString *utf8String = h5_url;
                NSString *title = name;
                NSString *description = [NSString stringWithFormat:@"%@%ld",name,(long)type];
                
                //创建发送对象实例
                SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                sendReq.bText = NO;//不使用文本信息
                sendReq.scene = 0;
                // 发送到聊天界面  0
                // 发送到朋友圈    1
                // 发送到微信收藏  2
                //创建分享内容对象
                WXMediaMessage *urlMessage = [WXMediaMessage message];
                urlMessage.title = title;//分享标题 urlMessage.description = @"";
                urlMessage.description= description;
                
                //分享描述
                [urlMessage setThumbImage:image];
                //分享图片,使用SDK的setThumbImage方法可压缩图片大小
                //创建多媒体对象
                WXWebpageObject *webObj = [WXWebpageObject object];
                webObj.webpageUrl = utf8String;//分享链接
                
                //完成发送对象实例
                urlMessage.mediaObject = webObj;
                sendReq.message = urlMessage;
                //发送分享信息
                [WXApi sendReq:sendReq];
            }
        }];
    } else if (type == 1) {
        
        NSString *utf8String = h5_url;
        NSString *title = name;
        NSString *description = [NSString stringWithFormat:@"%@%ld",name,(long)type];
        
        NSString *previewImageUrl = icon ;
        QQApiNewsObject *newsObj = [QQApiNewsObject
                                    objectWithURL:[NSURL URLWithString:utf8String]
                                    title:title
                                    description:description
                                    previewImageURL:[NSURL URLWithString:previewImageUrl]];
        req = [SendMessageToQQReq reqWithContent:newsObj];
        
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    }else if (type == 2) {
        SDWebImageManager *manager =[SDWebImageManager sharedManager];
        //根据URLString去下载图片
        NSString *URLString = icon;
        __weak typeof(self) weakSelf = self;
        [manager loadImageWithURL:[NSURL URLWithString:URLString] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
            
            if (finished) {
                NSLog(@" shuju %@",image);
                NSString *utf8String = h5_url;
                NSString *title = name;
                NSString *description = [NSString stringWithFormat:@"%@%ld",name,(long)type];
                
                //创建发送对象实例
                SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                sendReq.bText = NO;//不使用文本信息
                sendReq.scene = 1;
                // 发送到聊天界面  0
                // 发送到朋友圈    1
                // 发送到微信收藏  2
                //创建分享内容对象
                WXMediaMessage *urlMessage = [WXMediaMessage message];
                urlMessage.title = title;//分享标题 urlMessage.description = @"";
                urlMessage.description= description;
                
                //分享描述
                [urlMessage setThumbImage:image];
                //分享图片,使用SDK的setThumbImage方法可压缩图片大小
                //创建多媒体对象
                WXWebpageObject *webObj = [WXWebpageObject object];
                webObj.webpageUrl = utf8String;//分享链接
                
                //完成发送对象实例
                urlMessage.mediaObject = webObj;
                sendReq.message = urlMessage;
                //发送分享信息
                [WXApi sendReq:sendReq];
            }
        }];
    }else if (type == 3) {
        // 用来复制
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string =  h5_url;

    }
}
@end
